package SingletonMode;

/**
 * @program: DesignModeProject
 * @description: 饿汉模式（线程安全，同步方法）
 * @author: Li canzhang
 * @create: 2022-04-10 15:12
 **/
public class SingletonTest04 {
    public static void main(String[] args) {
        Singleton04 inst1 = Singleton04.getInst();
        Singleton04 inst2 = Singleton04.getInst();

        System.out.println(inst1.hashCode());
        System.out.println(inst2.hashCode());
    }
}

class Singleton04{
    private static Singleton04 inst;

    private Singleton04(){

    }
    //线程同步方法
    public static synchronized Singleton04 getInst(){
        if(inst == null){
            inst = new Singleton04();
        }
        return inst;
    }
}